package top.kaoshanji.p2ch10b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.u1constant.FileConstant;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * 代码清单-p2ch10b-325页a：从文件中读取Java对象
 * author kaoshanji
 * ^_^
 * create time 2025/2/3 10:25
 */
public class ReadUser {

    private static final Logger logger = LogManager.getLogger(ReadUser.class);

    public static void main(String[] args) {
        logger.info("...代码清单-p2ch10b-325页a：从文件中读取Java对象...");

        ReadUser readUser = new ReadUser();
        User user = readUser.readUser();

        logger.info("...User里email属性的值：{}...", user.getEmail());

    }

    public User readUser() {
        Path path = Paths.get(FileConstant.LOCAL_ROOT_PATH.concat("p2ch10b324页a\\user.bin"));

        try (ObjectInputStream input = new ObjectInputStream(Files.newInputStream(path))){
            return (User)input.readObject();
        } catch (IOException | ClassNotFoundException e) {
            throw new RuntimeException(e);
        }

    }

}
